/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.Functionary_controller;
import javax.swing.table.DefaultTableModel;
import Model.Functionary_model;
import Utilidades.Utilidades;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Molina
 */
public class Functionary_view extends javax.swing.JInternalFrame {

    DefaultTableModel table;
    boolean editable = false;
    private DefaultTableModel datos = null;
    private int numPage = 1;
    static Utilidades utilids = new Utilidades();
    /**
     * cantidad de pagina
     */
    private static int CANT_PAGE = -1;
    private static int NUM_RECORDS_PAGE = 4;

    /**
     * Creates new form Functionary_view
     */
    public Functionary_view() {
        initComponents();
        CargarTablaPaginada();
        utilids.cargarListas(combo_tipdocumento,3);
        utilids.cargarListas(combo_cargo,4);
        utilids.cargarListas(combo_estado,2);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        txtbuscar_funcionario = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablafuncionarios = new javax.swing.JTable();
        jLabelidFuncionario = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtdocumento_funcionario = new javax.swing.JTextField();
        txtapellido_funcionario = new javax.swing.JTextField();
        txtnombre_funcionario = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        btnAgregar = new javax.swing.JButton();
        btnActualizar = new javax.swing.JButton();
        bntNuevo = new javax.swing.JButton();
        combo_tipdocumento = new javax.swing.JComboBox();
        btnSiguiente = new javax.swing.JButton();
        btnUltimo = new javax.swing.JButton();
        msgNumPagina = new javax.swing.JLabel();
        btnAnterior = new javax.swing.JButton();
        btnPrimero = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txttelefono_funcionario = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        combo_cargo = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        txtemail_funcionario = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtextension_funcionario = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        combo_estado = new javax.swing.JComboBox();

        setClosable(true);
        setTitle("Funcionarios");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Gestión de Funcionarios", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel7.setText("Código Funcionario");
        jPanel3.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 120, 20));
        jPanel3.add(txtbuscar_funcionario, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 10, 160, -1));

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/find.png"))); // NOI18N
        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 10, 100, -1));

        jPanel1.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 670, 50));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos Basicos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tablafuncionarios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código Funcionario", "Tipo Documento", "Documento", "Nombre", "Apellido", "Cargo", "Telefono", "Email", "Extension", "Estado"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablafuncionarios.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablafuncionariosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tablafuncionarios);

        jPanel2.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 270, 630, 110));
        jPanel2.add(jLabelidFuncionario, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 30, 90, 20));

        jLabel3.setText("Documento");
        jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 70, 70, 20));

        jLabel4.setText("Telefono");
        jPanel2.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 160, -1, 20));

        jLabel5.setText("Apellido");
        jPanel2.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 70, -1, 20));

        jLabel6.setText("Nombre");
        jPanel2.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 110, -1, 20));

        txtdocumento_funcionario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtdocumento_funcionarioKeyReleased(evt);
            }
        });
        jPanel2.add(txtdocumento_funcionario, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 70, 110, -1));

        txtapellido_funcionario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtapellido_funcionarioKeyReleased(evt);
            }
        });
        jPanel2.add(txtapellido_funcionario, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 70, 110, -1));

        txtnombre_funcionario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtnombre_funcionarioKeyReleased(evt);
            }
        });
        jPanel2.add(txtnombre_funcionario, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 110, 110, -1));

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnAgregar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/add.png"))); // NOI18N
        btnAgregar.setText("Agregar");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });
        jPanel4.add(btnAgregar, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 110, 30));

        btnActualizar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnActualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/update.png"))); // NOI18N
        btnActualizar.setText("Actualizar");
        btnActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActualizarActionPerformed(evt);
            }
        });
        jPanel4.add(btnActualizar, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, 110, 30));

        bntNuevo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        bntNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/user_add.png"))); // NOI18N
        bntNuevo.setText("Nuevo");
        bntNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntNuevoActionPerformed(evt);
            }
        });
        jPanel4.add(bntNuevo, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 150, 110, 30));

        jPanel2.add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 30, 130, 200));

        combo_tipdocumento.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[Seleccione]" }));
        jPanel2.add(combo_tipdocumento, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 30, 110, -1));

        btnSiguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/resultset_next.png"))); // NOI18N
        btnSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSiguienteActionPerformed(evt);
            }
        });
        jPanel2.add(btnSiguiente, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 380, 40, 30));

        btnUltimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/resultset_last.png"))); // NOI18N
        btnUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUltimoActionPerformed(evt);
            }
        });
        jPanel2.add(btnUltimo, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 380, 40, 30));

        msgNumPagina.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        msgNumPagina.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        msgNumPagina.setText("# de #");
        jPanel2.add(msgNumPagina, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 380, 100, 20));

        btnAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/resultset_previous.png"))); // NOI18N
        btnAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnteriorActionPerformed(evt);
            }
        });
        jPanel2.add(btnAnterior, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 380, 40, 30));

        btnPrimero.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/resultset_first.png"))); // NOI18N
        btnPrimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrimeroActionPerformed(evt);
            }
        });
        jPanel2.add(btnPrimero, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 380, 40, 30));

        jLabel8.setText("Email");
        jPanel2.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 160, -1, 20));

        jLabel9.setText("Cargo");
        jPanel2.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 110, -1, 20));

        txttelefono_funcionario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txttelefono_funcionarioKeyReleased(evt);
            }
        });
        jPanel2.add(txttelefono_funcionario, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 160, 110, -1));

        jLabel10.setText("Tipo Documento");
        jPanel2.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 30, 100, 20));

        combo_cargo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[Seleccione]" }));
        jPanel2.add(combo_cargo, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 110, 110, -1));

        jLabel11.setText("Código Funcionario");
        jPanel2.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 30, 110, 20));
        jPanel2.add(txtemail_funcionario, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 160, 110, -1));

        jLabel1.setText("Extension");
        jPanel2.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 210, -1, 20));
        jPanel2.add(txtextension_funcionario, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 210, 110, -1));

        jLabel12.setText("Estado");
        jPanel2.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 210, -1, 20));

        combo_estado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[Seleccione]" }));
        jPanel2.add(combo_estado, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 210, 110, -1));

        jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, 670, 420));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, 690, 510));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tablafuncionariosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablafuncionariosMouseClicked
        // TODO add your handling code here:
        editable = true;
        if (evt.getClickCount() == 2) {
            if (tablafuncionarios.getSelectedRowCount() >= 0) {
                DefaultTableModel table = (DefaultTableModel) tablafuncionarios.getModel();
                //Asigna a un variable el valor de la tabla seleccionada
                int filamodificada = tablafuncionarios.getSelectedRow();
                //toma los valores de la tabla y los agrega a las cajas de texto
                jLabelidFuncionario.setText(table.getValueAt(filamodificada, 0).toString());
                combo_tipdocumento.setSelectedItem(table.getValueAt(filamodificada, 1).toString());
                txtdocumento_funcionario.setText(table.getValueAt(filamodificada, 2).toString());
                txtnombre_funcionario.setText(table.getValueAt(filamodificada, 3).toString());
                txtapellido_funcionario.setText(table.getValueAt(filamodificada, 4).toString());
                combo_cargo.setSelectedItem(table.getValueAt(filamodificada, 5).toString());
                txttelefono_funcionario.setText(table.getValueAt(filamodificada, 6).toString());
                txtemail_funcionario.setText(table.getValueAt(filamodificada, 7).toString());
                txtextension_funcionario.setText(table.getValueAt(filamodificada, 8).toString());
                combo_estado.setSelectedItem(table.getValueAt(filamodificada, 9).toString());
            }
        }
    }//GEN-LAST:event_tablafuncionariosMouseClicked

    private void txtdocumento_funcionarioKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtdocumento_funcionarioKeyReleased
        // TODO add your handling code here:
//        if (!txtdocumento_funcionario.getText().matches("[0-9]")) {
//            JOptionPane.showMessageDialog(null, "Datos invalido", "Aviso", JOptionPane.ERROR_MESSAGE);
//            txtdocumento_funcionario.setText("");
//        }
    }//GEN-LAST:event_txtdocumento_funcionarioKeyReleased

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        // TODO add your handling code here:
        String datos[] = new String[9];
        String acum = "";
        String tipdocumento = combo_tipdocumento.getSelectedItem().toString();
        String documentofuncionario = txtdocumento_funcionario.getText();
        String nombrefuncionario = txtnombre_funcionario.getText();
        String apellidofuncionario = txtapellido_funcionario.getText();
        String cargo = combo_cargo.getSelectedItem().toString();
        String telefonofuncionario = txttelefono_funcionario.getText();
        String emailfuncionario = txtemail_funcionario.getText();
        String extensionfuncionario = txtextension_funcionario.getText();
        String estado = combo_estado.getSelectedItem().toString();

        Functionary_controller funcionario = new Functionary_controller("", tipdocumento, documentofuncionario,
                nombrefuncionario, apellidofuncionario, cargo, telefonofuncionario, emailfuncionario, extensionfuncionario, estado);
        try {
            if (combo_tipdocumento.getSelectedItem().equals("[Seleccione]") || txtdocumento_funcionario.getText().equals("") || txtnombre_funcionario.getText().equals("")
                    || txtapellido_funcionario.getText().equals("") || combo_cargo.getSelectedItem().equals("[Seleccione]") || txttelefono_funcionario.getText().equals("")
                    || txtemail_funcionario.getText().equals("") || txtextension_funcionario.getText().equals("")
                    || combo_estado.getSelectedItem().equals("[Seleccione]")) {
                acum += "Algun campo le quedo incompleto, por favor llenelo" + ".\t\n";
                JOptionPane.showMessageDialog(null, acum);
                return;
            } else {
                System.out.println("Entro");
                if (Functionary_model.adicionarFuncionario(funcionario)) {
                    table = (DefaultTableModel) tablafuncionarios.getModel();
                    datos[0] = this.combo_tipdocumento.getSelectedItem().toString();
                    this.combo_tipdocumento.setSelectedItem(null);
                    datos[1] = this.txtdocumento_funcionario.getText();
                    this.txtdocumento_funcionario.setText(null);
                    datos[2] = this.txtnombre_funcionario.getText();
                    this.txtnombre_funcionario.setText(null);
                    datos[3] = this.txtapellido_funcionario.getText();
                    this.txtapellido_funcionario.setText(null);
                    datos[4] = this.combo_cargo.getSelectedItem().toString();
                    this.combo_cargo.setSelectedItem(null);
                    datos[5] = this.txttelefono_funcionario.getText();
                    this.txttelefono_funcionario.setText(null);
                    datos[6] = this.txtemail_funcionario.getText();
                    this.txtemail_funcionario.setText(null);
                    datos[7] = this.txtextension_funcionario.getText();
                    this.txtextension_funcionario.setText(null);
                    datos[8] = this.combo_cargo.getSelectedItem().toString();
                    this.combo_cargo.setSelectedItem(null);
                    table.addRow(datos);
                    limpiarTabla();
                    CargarTablaPaginada();
                    //JOptionPane.showMessageDialog(this, "Registro Adicionado");
                } else {
                    JOptionPane.showMessageDialog(this, "Registro No Adicionado", "", JOptionPane.ERROR_MESSAGE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void btnActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActualizarActionPerformed
        // TODO add your handling code here:
        String id = jLabelidFuncionario.getText();
        String tipdocumento = combo_tipdocumento.getSelectedItem().toString();
        String documentofuncionario = txtdocumento_funcionario.getText();
        String nombrefuncionario = txtnombre_funcionario.getText();
        String apellidofuncionario = txtapellido_funcionario.getText();
        String cargo = combo_cargo.getSelectedItem().toString();
        String telefonofuncionario = txttelefono_funcionario.getText();
        String emailfuncionario = txtemail_funcionario.getText();
        String extensionfuncionario = txtextension_funcionario.getText();
        String estado = combo_estado.getSelectedItem().toString();

        String acum = "";
        try {
            if (combo_tipdocumento.getSelectedItem().equals("[Seleccione]") || txtdocumento_funcionario.getText().equals("") || txtnombre_funcionario.getText().equals("")
                    || txtapellido_funcionario.getText().equals("") || combo_cargo.getSelectedItem().equals("[Seleccione]") || txttelefono_funcionario.getText().equals("")
                    || txtemail_funcionario.getText().equals("") || txtextension_funcionario.getText().equals("")
                    || combo_estado.getSelectedItem().equals("[Seleccione]")) {
                acum += "Algun campo le quedo incompleto, por favor llenelo" + ".\t\n";
                JOptionPane.showMessageDialog(null, acum);
                return;
            }
            Functionary_controller funcionario = new Functionary_controller(id, tipdocumento, documentofuncionario,
                    nombrefuncionario, apellidofuncionario, cargo, telefonofuncionario, emailfuncionario, extensionfuncionario, estado);
            Functionary_model.modificarFuncionario(funcionario);
            limpiarTabla();
            CargarTablaPaginada();
            //JOptionPane.showMessageDialog(this, "Registro Actualizado");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_btnActualizarActionPerformed

    private void bntNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntNuevoActionPerformed
        // TODO add your handling code here:
        jLabelidFuncionario.setText("");
        combo_tipdocumento.setSelectedItem("[Seleccione]");
        txtdocumento_funcionario.setText("");
        txtnombre_funcionario.setText("");
        txtapellido_funcionario.setText("");
        combo_cargo.setSelectedItem("[Seleccione]");
        txttelefono_funcionario.setText("");
        txtemail_funcionario.setText("");
        txtextension_funcionario.setText("");
        combo_estado.setSelectedItem("[Seleccione]");
    }//GEN-LAST:event_bntNuevoActionPerformed

    private void btnSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSiguienteActionPerformed
        // TODO add your handling code here:
        if (numPage < CANT_PAGE) {
            numPage++;
        }
        limpiarTabla();
        CargarTablaPaginada();
    }//GEN-LAST:event_btnSiguienteActionPerformed

    private void btnUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUltimoActionPerformed
        // TODO add your handling code here:
        numPage = CANT_PAGE;
        limpiarTabla();
        CargarTablaPaginada();
    }//GEN-LAST:event_btnUltimoActionPerformed

    private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnteriorActionPerformed
        // TODO add your handling code here:
        if (numPage > 1) {
            numPage--;
        }
        limpiarTabla();
        CargarTablaPaginada();
    }//GEN-LAST:event_btnAnteriorActionPerformed

    private void btnPrimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrimeroActionPerformed
        // TODO add your handling code here:
        numPage = 1;
        limpiarTabla();
        CargarTablaPaginada();
    }//GEN-LAST:event_btnPrimeroActionPerformed

    private void txttelefono_funcionarioKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txttelefono_funcionarioKeyReleased
        // TODO add your handling code here:
//        if (!txttelefono_funcionario.getText().matches("[0-9]")) {
//            JOptionPane.showMessageDialog(null, "Datos invalido", "Aviso", JOptionPane.ERROR_MESSAGE);
//            txttelefono_funcionario.setText("");
//        }
    }//GEN-LAST:event_txttelefono_funcionarioKeyReleased

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String buscar = txtbuscar_funcionario.getText();
        Functionary_controller funcionario = Functionary_model.buscarFuncionario(buscar);

        if (txtbuscar_funcionario.getText().equals("")) {
            javax.swing.JOptionPane.showMessageDialog(this, "Digite el Registro a Buscar");
            return;
        } else {
            if (funcionario != null) {
                MoverDatos(funcionario);
                txtbuscar_funcionario.setText("");
            } else {
                JOptionPane.showMessageDialog(this, "No Encontro al Registro");
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void txtnombre_funcionarioKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtnombre_funcionarioKeyReleased
        // TODO add your handling code here:
//        if (!txtnombre_funcionario.getText().matches("[a-zA-Z]*")) {
//            JOptionPane.showMessageDialog(null, "Datos invalido", "Aviso", JOptionPane.ERROR_MESSAGE);
//            txtnombre_funcionario.setText("");
//        }
    }//GEN-LAST:event_txtnombre_funcionarioKeyReleased

    private void txtapellido_funcionarioKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtapellido_funcionarioKeyReleased
        // TODO add your handling code here:
//        if (!txtapellido_funcionario.getText().matches("[a-zA-Z]*")) {
//            JOptionPane.showMessageDialog(null, "Datos invalido", "Aviso", JOptionPane.ERROR_MESSAGE);
//            txtapellido_funcionario.setText("");
//        }
    }//GEN-LAST:event_txtapellido_funcionarioKeyReleased

    public void MoverDatos(Functionary_controller funcionario) {
        jLabelidFuncionario.setText(funcionario.getId_funcionario());
        combo_tipdocumento.setSelectedItem(funcionario.getTipdocument_funcionario());
        txtdocumento_funcionario.setText(funcionario.getDocument_funcionario());
        txtnombre_funcionario.setText(funcionario.getNombre_funcionario());
        txtapellido_funcionario.setText(funcionario.getApellido_funcionario());
        combo_cargo.setSelectedItem(funcionario.getCargo_funcionario());
        txttelefono_funcionario.setText(funcionario.getTelefono_funcionario());
        txtemail_funcionario.setText(funcionario.getEmail_funcionario());
        txtextension_funcionario.setText(funcionario.getExtension_funcionario());
        combo_estado.setSelectedItem(funcionario.getEstado_funcionario());
    }

    public void CargarTablaPaginada() {
        try {
            ArrayList<Object[]> data = new ArrayList<Object[]>();
            Utilidades pag = new Utilidades();
            String consulta = "SELECT fun_numero, fun_tipodocumento, fun_documento, fun_nombre, fun_apellido, "
                    + "fun_cargo, fun_telefono, fun_email, fun_extension, fun_estado FROM "
                    + "funcionarios";
            List lista = pag.ejecutaQueryPaginada(consulta, numPage, NUM_RECORDS_PAGE);
            Integer numeroRegistros = (Integer) lista.get(0);
            float tempNuumPage = (float) numeroRegistros / NUM_RECORDS_PAGE;
            CANT_PAGE = Math.round(tempNuumPage);//((int)tempNuumPage + (Math.round(tempNuumPage)%2 == 0 ? 0 : 1));
            // System.out.println("NUMERO DE REGISTROS TOTALES:" + numeroRegistros.toString());
            msgNumPagina.setText("Pagina " + numPage + " de " + CANT_PAGE);
            /*
             * for (int i = 1; i < lista.size(); i++) { List lista1 = (List)
             * lista.get(i); System.out.println(lista1.toString()); }
             */
            ArrayList unRegistro = null;
            for (int i = 2; i < lista.size(); i++) {
                unRegistro = (ArrayList) lista.get(i);
                Object[] rows = new Object[unRegistro.size()];
                for (int j = 0; j < unRegistro.size(); j++) {
                    rows[j] = unRegistro.get(j);
                }
                data.add(rows);
            }
            table = (DefaultTableModel) this.tablafuncionarios.getModel();
            for (int i = 0; i < data.size(); i++) {
                table.addRow(data.get(i));
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, e.getMessage());
        }
    }

    public void limpiarTabla() {
        table = (DefaultTableModel) tablafuncionarios.getModel();
        try {
            int size = table.getRowCount();
            for (int i = size - 1; i >= 0; i--) {
                table.removeRow(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bntNuevo;
    private javax.swing.JButton btnActualizar;
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnAnterior;
    private javax.swing.JButton btnPrimero;
    private javax.swing.JButton btnSiguiente;
    private javax.swing.JButton btnUltimo;
    private javax.swing.JComboBox combo_cargo;
    private javax.swing.JComboBox combo_estado;
    private javax.swing.JComboBox combo_tipdocumento;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelidFuncionario;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel msgNumPagina;
    private javax.swing.JTable tablafuncionarios;
    private javax.swing.JTextField txtapellido_funcionario;
    private javax.swing.JTextField txtbuscar_funcionario;
    private javax.swing.JTextField txtdocumento_funcionario;
    private javax.swing.JTextField txtemail_funcionario;
    private javax.swing.JTextField txtextension_funcionario;
    private javax.swing.JTextField txtnombre_funcionario;
    private javax.swing.JTextField txttelefono_funcionario;
    // End of variables declaration//GEN-END:variables
}
